<?php
namespace backend\controllers;

use Yii;
use yii\db\Exception;
use yii\web\AdminController;


class ShopGroupController extends AdminController
{

    public $layout = false;
    //业务关键字
    public $service = 'shopGroup';

    public function actionIndex()
    {
        $sql = 'select * from j_shop';
        $shop = Yii::$app->db->createCommand($sql)->queryAll();

        return $this->render('index', ['shop' => $shop]);
    }


    public function actionGetList()
    {
        $data = Yii::$app->request->post();
        $sql = '';
        $sql_list = 'SELECT * FROM j_shop_group WHERE 1=1 ';
        $sql_count = 'SELECT COUNT(0) CNT FROM j_shop_group WHERE 1=1 ';

        $condition = $where = array();
        $searchName = trim($data['name']);
        if (!empty($searchName)) {
            $sql .= ' And name like "%' . $searchName . '%"';
        }


        $list_count = Yii::$app->db->createCommand($sql_count . $sql)->queryOne();
        $count = $list_count['CNT'];


        if ($data['start'] || $data['length']) {
            $sql .= ' limit ' . $data['start'] . ',' . $data['length'];
        }

        $list = Yii::$app->db->createCommand($sql_list . $sql)->queryAll();
        if (!empty($list)) {
            //存储类型
//            $config_where = ['config_group_id' => 'storeType', 'use_flg' => 1];
//            $storeType = SysConfig::find()->where($config_where)->select('config_key,config_value')->asArray()->all();
//            $storeArr = array();
//            foreach ($storeType as $s) {
//                $storeArr[$s['config_key']] = $s['config_value'];
//            }
//            foreach ($list as &$val) {
//                $val['storeName'] = isset($storeArr[$val['storeType']]) ? $storeArr[$val['storeType']] : '';
//            }
        } else {
            $list = array();
        }

        //相关参数 http://datatables.club/manual/server-side.html
        $result['draw'] = intval($data['draw']);
        $result['recordsTotal'] = intval($count);
        $result['recordsFiltered'] = intval($count);
        $result['data'] = $list;

        return \yii\helpers\Json::encode($result);
    }


    public function actionShopGroupDtl()
    {
        $data = Yii::$app->request->post();
        $sql = 'select * from j_shop_group where id=' . $data['id'];
        $result = Yii::$app->db->createCommand($sql)->queryOne();
        $this->retJson('ok', 200, $result);

    }

    public function actionAddShopGroup()
    {

        $data = Yii::$app->request->post();
        $model = array();
        $model['id'] = $data['id'];
        $model['name'] = $data['name'];
        $model['remark'] = $data['remark'];
        if ($data['shopIds']) {
            $model['shopIds'] = implode(",", $data['shopIds']);
        } else {

            $this->retJson('请选择门店', 500);
        }


        if ($model['id']) {


            $ckSql = "select count(0) cnt from j_shop_group where id<>" . $data['id'] . " and name='" . $data['name'] . "'";
            $ck_count = Yii::$app->db->createCommand($ckSql)->queryOne();
            if ($ck_count && $ck_count['cnt']) {
                $this->retJson('组名称已存在', 500);
            }
            $model['editor'] = $this->getCreator();
            $model['edit_time'] = date('Y-m-d H:i:s');
            Yii::$app->db->createCommand()->update('j_shop_group', $model, ['id' => $data['id']])->execute();
        } else {

            $ckSql = "select count(0) cnt from j_shop_group where name='" . $data['name'] . "'";
            $ck_count = Yii::$app->db->createCommand($ckSql)->queryOne();
            if ($ck_count && $ck_count['cnt']) {
                $this->retJson('组名称已存在', 500);
            }

            $model['creator'] = $this->getCreator();
            $model['create_time'] = date('Y-m-d H:i:s');
            Yii::$app->db->createCommand()->insert('j_shop_group', $model)->execute();
        }

        $this->retJson('操作成功', 200);

    }

    public function getCreator()
    {
        $userInfo = \common\helps\tools::getloginUserInfo();
        if ($userInfo) {
            $userName = $userInfo->userName;
        } else {
            $userName = '';
        }
        return $userName;
    }
}